package demo24;

//二叉搜索树
public class BinarySearchTree {
    //创造节点
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val=val;
        }
    }
    public TreeNode root;//根节点
    //插入节点
    public void insert(int key){
        if(root==null){
            root=new TreeNode(key);
            return;
        }
        TreeNode cur=root;
        TreeNode parent=null;
        TreeNode node=new TreeNode(key);
        while(cur!=null){//找到key应去的方向
            if(cur.val<key){
                parent=cur;
                cur=cur.right;
            } else if (cur.val>key) {
                parent=cur;
                cur=cur.left;
            }else{
                return;
            }
        }
        if(parent.val<key){//根据其父节点值判断该存放在何处
            parent.right=node;
        }else{
            parent.left=node;
        }
    }
    //搜索节点,比较与根节点的大小，利用cur遍历左或右，直到找到或遍历完
    public TreeNode search(int val){
        TreeNode cur=root;
        while(cur!=null){
            if(cur.val<val){
                cur=cur.right;
            } else if (cur.val>val) {
                cur=cur.left;
            }else{
                System.out.println("找到了！");
                System.out.println("在这样的树中：");
                display(root);
                return cur;
            }
        }
        display(root);
        return null;
    }
    //删除节点（删除节点1.左为空或2.右为空或3.左右都不为空）首先先找到其父节点与自己
    public void remove(int key){
        TreeNode cur=root;
        TreeNode parent=null;
        while(cur!=null){
            if(cur.val<key){
                parent=cur;
                cur=cur.right;
            }else if(cur.val>key){
                parent=cur;
                cur=cur.left;
            }else{
                removeNode(parent,cur);
                return;
            }
        }
        System.out.println("删除后的树:");
        display(root);
    }
    public void removeNode(TreeNode parent,TreeNode cur){
        //节点左边为空
        if(cur.left==null){
            if(cur==root){
                root=cur.right;
            }else if(parent.left==cur){
                parent.left=cur.right;
            }else{
                parent.right=cur.right;
            }
        }else if(cur.right==null)//节点右边为空
        {
            if(cur==root){
                root=cur.left;
            }else if(parent.left==cur){
                parent.left=cur.left;
            }else{
                parent.right=cur.left;
            }
        }else{      //节点左右边不为空
            TreeNode tp=cur;
            TreeNode t=cur.right;
            while(t.left!=null){
                tp=t;
                t=t.left;
            }
            cur.val=t.val;
            if(tp.left==t){
                tp.left=t.right;
            }else{
                tp.right=t.right;
            }

        }
        display(root);
    }
    //打印
    public TreeNode display(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode cur = root;
        System.out.print(cur.val + " ");
        display(root.left);
        display(root.right);
        return null;
    }
}
